/**
 * 0: 成功
 * 1: 失败
 * 2: 未登录
 * 3: 无权限
 * 4: 其他错误
 */
type CodeType = 0 | 1 | 2 | 3 | 4;

interface ResponseType<T> {
  code: CodeType;
  message: string;
  data: T;
}

export interface GenerateResponseParams<T> {
  data: T;
  message?: string;
  code?: CodeType;
}

/**
 * 创建响应
 * @param params 响应参数
 * @returns 响应
 */
export const generateResponse = <T>(params: GenerateResponseParams<T>): ResponseType<T> => {
  const { data, message = 'success', code = 0 } = params;

  return {
    code,
    message,
    data,
  };
};
